import Vue from 'vue'
import Vuex from 'vuex'
import { getUserinfoAPI } from '@/api/user.js'

Vue.use(Vuex)

export default new Vuex.Store({
  state: {
    token: window.localStorage.getItem('token') || '',
    refresh_token: window.localStorage.getItem('refresh_token') || '',
    user: JSON.parse(localStorage.getItem('user')) || {}
  },
  mutations: {
    setUser(state, userObj) {
      state.user = userObj
      localStorage.setItem('user', JSON.stringify(userObj))
      console.log(state.user)
    },
    setPhoto(state, photo) {
      state.user.photo = photo
    },
    setName(state, name) {
      state.user.name = name
    },
    setToken(state, val) {
      state.token = val
      localStorage.setItem('token', val)
    },
    setRefresh_Token(state, val) {
      state.refresh_token = val
      localStorage.setItem('refresh_token', val)
    }
  },
  actions: {
    async getUserinfo(store) {
      const res = await getUserinfoAPI()
      store.commit('setUser', res.data.data)
    }
  },
  getters: {
    userPhoto(state) {
      if (state.user.photo === undefined) {
      }
      return state.user.photo
    }
  },
  modules: {}
})
